<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2019/3/18
 * Time: 16:19
 */

namespace app\admin\model;


class Category extends Common{
    protected $table="Category";
    public function getTree()
    {
        $rows = $this->where("status", 1)->order('id')->select();
        $data = $this->tree($rows, 0, 0);
        return $data;
    }

    public function tree($rows, $pid = 0, $level = 0)
    {
        static $tree = [];
        foreach ($rows as $row) {
            if ($row['pid'] == $pid) {
                $row['level'] = $level;
                $tree[] = $row;
                $this->tree($rows, $row['id'], $level + 1);
            }
        }
        return $tree;
    }
}